/**
 * 
 */
package com.dsteiner.widgets.client;

import com.dsteiner.widgets.client.Widgets.ButtonExt;
import com.dsteiner.widgets.client.Widgets.FormPanelExt;
import com.dsteiner.widgets.client.Widgets.HTMLExt;
import com.dsteiner.widgets.client.Widgets.LazyWidget;
import com.dsteiner.widgets.client.controller.FileUploadController;
import com.dsteiner.widgets.client.model.FileUploadModel;
import com.dsteiner.widgets.client.resources.WidgetsResources;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;

public class FileUploadWidget extends LazyWidget{
	
	private final FileUploadModel fileUploadModel;
	private FileUploadController fileUploadController = new FileUploadController();

	public FileUploadWidget(FileUploadModel fileUploadModel) {
		this.fileUploadModel = fileUploadModel;
	}
	
	@Override
	public Widget createContentWidget() {
		FormPanelExt formPanel = new FormPanelExt();
		FileUpload fileUpload = new FileUpload();
		FlowPanel formFlowPanel = new FlowPanel();
		ButtonExt uploadFotoButton = new ButtonExt("enviar");
		HTMLExt titleHtml = new HTMLExt();
		HTMLExt maxFileSizeHtml = new HTMLExt();
		HTMLExt headerHtml = new HTMLExt("Subir archivo propio");
		
		fileUpload.setName("uploadFormElement");
		formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
		formPanel.setMethod(FormPanel.METHOD_POST);
		
		formFlowPanel.add(headerHtml);
		formFlowPanel.add(titleHtml);
		formFlowPanel.add(maxFileSizeHtml);
		formFlowPanel.add(fileUpload);
		formFlowPanel.add(uploadFotoButton);
		
		formPanel.setWidget(formFlowPanel);
		
		fileUpload.getElement().getStyle().setProperty("display", "block");
	
		setStyleName(WidgetsResources.INSTANCE.css().fileUploadWidget());
		maxFileSizeHtml.addStyleName(WidgetsResources.INSTANCE.css().maxFileSizeHtml());
		headerHtml.addStyleName(WidgetsResources.INSTANCE.css().headerHtml());
		
		fileUploadController.registerMaxFileSizeHtml(maxFileSizeHtml);
		fileUploadController.registerFileUploadModel(fileUploadModel);
		fileUploadController.registerFileUploadWidget(this);
		fileUploadController.registerFormPanel(formPanel);
		fileUploadController.registerTitleHtml(titleHtml);
		fileUploadController.registerUploadFotoButton(uploadFotoButton);
		
		return formPanel;
	}
}